<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.initChart(val)
      }
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(val) {
      if (this.chart) {
        this.chart.dispose()
      }
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(val)
    },
    setOptions({ incomeData, disburseData, differenceData, xAxisData, meaning } = {}) {
      this.chart.setOption({
        title: {
          text: ''
        },
        animation: true,
        animationDuration: 1500,
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        legend: {
          data: meaning
        },
        grid: {
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: xAxisData,
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [
          {
            name: meaning[0],
            type: 'bar',
            data: incomeData,
            itemStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: '#fec9bf'
                },
                {
                  offset: 1,
                  color: '#FF8871'
                }
              ])
            }
          }, {
            name: meaning[1],
            type: 'bar',
            data: disburseData,
            itemStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: '#619DFF'
                },
                {
                  offset: 1,
                  color: '#468CFF'
                }
              ])
            }
          }, {
            name: meaning[2],
            type: 'bar',
            data: differenceData,
            itemStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: '#dac2e5'
                },
                {
                  offset: 1,
                  color: '#e676d2'
                }
              ])
            }
          }
        ]
      })
    }
  }
}
</script>
